package com.fsu.database.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.fsu.database.dao.Author;
import com.fsu.database.dao.Book;
import com.fsu.database.dao.CastMember;
import com.fsu.database.dao.DVD;
import com.fsu.database.dao.DatabaseManager;
import com.fsu.database.dao.Publisher;

@SuppressWarnings("serial")
public class AdminPanel extends JDialog {
	
	private JComboBox<String> filterCb;
	
	private JPanel mainPanel;
	private JPanel titlePanel;
	private JPanel yearPanel;
	private JPanel pricePanel;
	private JPanel isbnPanel;
	private JPanel subjectPanel;
	private JPanel directorPanel;
	private JPanel genrePanel;
	private JPanel pubNamePanel;
	private JPanel pubUrlPanel;
	private JPanel authNamePanel;
	private JPanel authAddyPanel;
	private JPanel castNamePanel;
	private JPanel castAddyPanel;
	private JPanel basePanel;
	private JPanel bookPanel;
	private JPanel dvdPanel;
	
	private JLabel titleLabel;
	private JLabel yearLabel;
	private JLabel priceLabel;
	private JLabel isbnLabel;
	private JLabel subjectLabel;
	private JLabel pubNameLabel;
	private JLabel pubUrlLabel;
	private JLabel authNameLabel;
	private JLabel authAddyLabel;
	private JLabel directorLabel;
	private JLabel genreLabel;
	private JLabel castNameLabel;
	private JLabel castAddyLabel;
	
	private JTextField titleTf;
	private JTextField yearTf;
	private JTextField priceTf;
	private JTextField isbnTf;
	private JTextField subjectTf;
	private JTextField pubNameTf;
	private JTextField pubUrlTf;
	private JTextField authNameTf;
	private JTextField authAddyTf;
	private JTextField directorTf;
	private JTextField genreTf;
	private JTextField castNameTf;
	private JTextField castAddyTf;
	
	
	public AdminPanel(JFrame parent) {
		super(parent);
		init();
	}
	
	private void init() {
		final Toolkit tk = Toolkit.getDefaultToolkit();
		final Dimension size = tk.getScreenSize();
		
		final JButton addButton = new JButton("Add");
		
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String selection = (String) filterCb.getSelectedItem();
				try {
					if (selection.equals("Books")) {
						ArrayList<Author> authors = new ArrayList<Author>();
						authors.add(new Author(authNameTf.getText(), authAddyTf.getText()));
						Book book = new Book(titleTf.getText(), 
								Integer.parseInt(yearTf.getText()), 
								Float.parseFloat(priceTf.getText()), 
								isbnTf.getText(), 
								subjectTf.getText(), 
								new Publisher(pubUrlTf.getText(), pubNameTf.getText(), "", ""), 
								authors);
					
					
						DatabaseManager.addBook(book);
					} else {
						ArrayList<CastMember> cast = new ArrayList<CastMember>();
						cast.add(new CastMember(castNameTf.getText(), castAddyTf.getText()));
						DVD dvd = new DVD(titleTf.getText(), 
								Integer.parseInt(yearTf.getText()), 
								Float.parseFloat(priceTf.getText()), 
								directorTf.getText(), 
								genreTf.getText(), 
								cast);
						DatabaseManager.addDvd(dvd);
					}
					
					titleTf.setText("");
					yearTf.setText("");
					priceTf.setText("");
					isbnTf.setText("");
					subjectTf.setText("");
					pubNameTf.setText("");
					pubUrlTf.setText("");
					authNameTf.setText("");
					authAddyTf.setText("");
					directorTf.setText("");
					genreTf.setText("");
					castNameTf.setText("");
					castAddyTf.setText("");
					
				} catch (SQLException ex) {
					JOptionPane.showMessageDialog(AdminPanel.this, "Request failed. Please make sure you are connected to the internet and try again.");
					System.out.println("SQL Error " + ex.getErrorCode() + ": " + ex.getMessage());
					ex.printStackTrace();
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(AdminPanel.this, "Media already exists.");
					ex.printStackTrace();
				}
			}
		});
		
		mainPanel = new JPanel();
		basePanel = createBasePanel();
		bookPanel = createBookPanel();
		dvdPanel = createDVDPanel();
		
		mainPanel.add(basePanel);
		mainPanel.add(bookPanel);
		add(mainPanel, BorderLayout.CENTER);
		add(addButton, BorderLayout.SOUTH);
		setSize(600, 400);
		setLocation(size.width/3, size.height/3);
		
		setVisible(true);
	}

	private JPanel createBasePanel() {
		filterCb = new JComboBox<String>(new String[] { "Books", "DVDs" });
		
		filterCb.addActionListener(new ActionListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void actionPerformed(ActionEvent e) {
				
				JComboBox<String> cb = (JComboBox<String>) e.getSource();
				String selection = (String) cb.getSelectedItem();
				
				if (selection.equals("Books")) {
					mainPanel.remove(dvdPanel);
					//mainPanel.remove(bookPanel);
					mainPanel.add(bookPanel);
				} else if (selection.equals("DVDs")) {
					mainPanel.remove(bookPanel);
					mainPanel.add(dvdPanel);
				} else {
					mainPanel.remove(bookPanel);
					mainPanel.remove(dvdPanel);
				}
				mainPanel.updateUI();
			}
		});
		
		titleLabel = new JLabel("Title: ");
		yearLabel = new JLabel("Year: ");
		priceLabel = new JLabel("Price: ");
		
		titleTf = new JTextField(20);
		yearTf = new JTextField(20);
		priceTf = new JTextField(20);
		
		titlePanel = new JPanel();
		yearPanel = new JPanel();
		pricePanel = new JPanel();
		
		titlePanel.add(titleLabel);
		titlePanel.add(titleTf);
		
		yearPanel.add(yearLabel);
		yearPanel.add(yearTf);
		
		pricePanel.add(priceLabel);
		pricePanel.add(priceTf);
		
		JPanel tmp = new JPanel(new GridLayout(4, 1));
		tmp.add(filterCb);
		tmp.add(titlePanel);
		tmp.add(yearPanel);
		tmp.add(pricePanel);
		
		return tmp;
	}
	
	private JPanel createBookPanel() {
		isbnLabel = new JLabel("ISBN: ");
		subjectLabel = new JLabel("Subject: ");
		pubNameLabel = new JLabel("Publisher Name: ");
		pubUrlLabel = new JLabel("Publisher URL: ");
		authNameLabel = new JLabel("Author Name: ");
		authAddyLabel = new JLabel("Author Address: ");
		
		isbnTf = new JTextField(20);
		subjectTf = new JTextField(20);
		pubNameTf = new JTextField(20);
		pubUrlTf = new JTextField(20);
		authNameTf = new JTextField(20);
		authAddyTf = new JTextField(20);
	
		isbnPanel = new JPanel();
		subjectPanel = new JPanel();
		pubNamePanel = new JPanel();
		pubUrlPanel = new JPanel();
		authNamePanel = new JPanel();
		authAddyPanel = new JPanel();
		
		isbnPanel.add(isbnLabel);
		isbnPanel.add(isbnTf);
		
		subjectPanel.add(subjectLabel);
		subjectPanel.add(subjectTf);
		
		pubNamePanel.add(pubNameLabel);
		pubNamePanel.add(pubNameTf);
		
		pubUrlPanel.add(pubUrlLabel);
		pubUrlPanel.add(pubUrlTf);
		
		authNamePanel.add(authNameLabel);
		authNamePanel.add(authNameTf);
		
		authAddyPanel.add(authAddyLabel);
		authAddyPanel.add(authAddyTf);
		
		JPanel tmp = new JPanel(new GridLayout(6, 1));
		tmp.add(isbnPanel);
		tmp.add(subjectPanel);
		tmp.add(pubNamePanel);
		tmp.add(pubUrlPanel);
		tmp.add(authNamePanel);
		tmp.add(authAddyPanel);
		
		return tmp;
	}
	
	private JPanel createDVDPanel() {
		directorLabel = new JLabel("Director: ");
		genreLabel = new JLabel("Genre: ");
		castNameLabel = new JLabel("Cast Name: ");
		castAddyLabel = new JLabel("Cast Address: ");
		
		directorTf = new JTextField(20);
		genreTf = new JTextField(20);
		castNameTf = new JTextField(20);
		castAddyTf = new JTextField(20);
		
		
		directorPanel = new JPanel();
		genrePanel = new JPanel();
		castNamePanel = new JPanel();
		castAddyPanel = new JPanel();
		
		directorPanel.add(directorLabel);
		directorPanel.add(directorTf);
		
		genrePanel.add(genreLabel);
		genrePanel.add(genreTf);
		
		castNamePanel.add(castNameLabel);
		castNamePanel.add(castNameTf);
		
		castAddyPanel.add(castAddyLabel);
		castAddyPanel.add(castAddyTf);
		
		JPanel tmp = new JPanel(new GridLayout(4, 1));
		tmp.add(directorPanel);
		tmp.add(genrePanel);
		tmp.add(castNamePanel);
		tmp.add(castAddyPanel);
		
		return tmp;
	}
	
}
